<?php

/**
 * 后台通知
 * File Name：AdminMessage.php
 * File Encoding：UTF-8
 * File New Time：2014-10-8 14:09:37
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class AdminMessage extends BaseModel {

    //通知类型
    const system = 0; //系统通知
    const transfer = 1; //用户类型转换
    const goods = 2; //商品相关，如 审核通知
    const punish = 3;//商品举报

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 发送一个后台通知
     * @param type $admin_id 接收用户UID
     * @param type $sendUser 产生通知的用户信息，字符串是用户名，数字是UID，数组是必须包含 uid,username
     * @param type $typeId
     * @param type $extendParams
     * @return boolean
     */
    public function notice($admin_id, $sendUser, $typeId, $extendParams = array()) {
        if (empty($admin_id)) {
            return false;
        }
        //开发环境下直接true
        if (defined('YII_DEBUG') && YII_DEBUG) {
            return true;
        }
        $data = array(
            'admin_id' => $admin_id,
            'sendUser' => $sendUser,
            'typeId' => $typeId,
            'extendParams' => $extendParams,
        );
        RedisCluster::getInstance()->push($this->getKey($admin_id), $data);
        return true;
    }

    /**
     * 获取KEY
     * @param type $admin_id
     * @return type
     */
    public function getKey($admin_id) {
        return "snsAdminMessage_{$admin_id}";
    }

    /**
     * 获取通知内容
     * @param type $admin_id
     * @return boolean
     */
    public function getNotice($admin_id) {
        if (empty($admin_id)) {
            return false;
        }
        //开发环境下直接false
        if (defined('YII_DEBUG') && YII_DEBUG) {
            return false;
        }
        $infoList = RedisCluster::getInstance()->pop($this->getKey($admin_id));
        if (empty($infoList)) {
            return false;
        }
        return $infoList;
    }

}
